package com.atguigu.crowd.funding.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atguigu.crowd.funding.entity.Admin;
import com.atguigu.crowd.funding.entity.AdminExample;
import com.atguigu.crowd.funding.mapper.AdminMapper;
import com.atguigu.crowd.funding.service.api.AdminService;
import com.atguigu.crowd.funding.util.CorwdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class AdminServiceImpl implements AdminService {
	@Autowired
	private AdminMapper adminMapper;
	
	@Override
	public List<Admin> getAll() {
		return adminMapper.selectByExample(new AdminExample());
	}

	@Override
	public void updateAdmin(Admin admin) {
		//adminMapper.updateByPrimaryKey(admin);
		adminMapper.updateByPrimaryKey(new Admin(2,"xm33","xm11","С��","000",""));
		System.out.println(10/0);
		adminMapper.updateByPrimaryKey(new Admin(3,"xm33","xm22","С��","000",""));
	}

	@Override
	public Admin login(String loginAcct,String userPswd) {
		// TODO Auto-generated method stub
		//����Admin��ѯ�����������ʺŲ�ѯ
		AdminExample adminE=new AdminExample();
		adminE.createCriteria().andLoginAcctEqualTo(loginAcct);
		
		//�����ʺŲ�ѯ������
		List<Admin> list=adminMapper.selectByExample(adminE);
		
		if(!CorwdUtil.CollectionEff(list)){
			//�жϲ�ѯ�Ľ���Ƿ�Ϊ��
			return null;
		}
		Admin admin=list.get(0);
		if(admin==null)
			return null;
		
		String md5pwd=CorwdUtil.md5(userPswd);	//�����������м���
		String DBpwd=admin.getUserPswd();		//��ȡ���ݿ��е�����
		
		if(Objects.equals(md5pwd, DBpwd)){
			// ����������������ô˵����¼�ܹ��ɹ�������Admin����
			return admin;
		}
		
		
		
		return null;
	}

	@Override
	public PageInfo<Admin> queryAdminByKeyWord(Integer pageNum, Integer pageSize, String keyWord) {

		//������ҳ���,����pagehelper���߷���
		PageHelper.startPage(pageNum, pageSize);
		
		//��ѯ
		List<Admin> list = (List<Admin>) adminMapper.queryAdminByKeyWord(keyWord);
		
		//��װ��pageinfo ����
		return new PageInfo<>(list);
	}

	@Override
	public void batchDelect(List<Integer> adminidlist) {
		// TODO Auto-generated method stub
		//ʹ��*Example�ഴ��sqlwhere����
		AdminExample AE=new AdminExample();
		AE.createCriteria().andIdIn(adminidlist);
		//ִ��ɾ��
		adminMapper.deleteByExample(AE);
	}

	@Override
	public void save(Admin admin) {
		// TODO Auto-generated method stub
		String pwd=admin.getUserPswd();
		//密码加密
		pwd=CorwdUtil.md5(pwd);
		admin.setUserPswd(pwd);
		adminMapper.insert(admin);
		
	}

	@Override
	public Admin queryAdminById(Integer adminid) {
		// TODO Auto-generated method stub
		Admin admin=adminMapper.selectByPrimaryKey(adminid);
		return admin;
	}

	@Override
	public void update(Admin admin) {
		// TODO Auto-generated method stub
		String pwd=CorwdUtil.md5(admin.getUserPswd());
		admin.setUserPswd(pwd);
		adminMapper.updateByPrimaryKey(admin);

	}

}
